package com.up.interview.flowable.controller2;

/**
 * @author: liuchengjun
 * @title: LeaveController
 * @projectName: 海联技术中台V2.0
 * @depart 软件事业部
 * @description:
 * @date: 2025/10/30/周四 16:13 (星期四)
 * @since V2.0
 */

import com.up.interview.flowable.entity.LeaveRequest;
import com.up.interview.flowable.service.LeaveService;
import lombok.RequiredArgsConstructor;
import org.flowable.task.api.Task;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 请假申请 REST API
 */
@RestController
@RequestMapping("/api/leave")
@RequiredArgsConstructor
public class LeaveController {

    private final LeaveService leaveService;

    /**
     * 提交请假申请
     */
    @PostMapping("/submit")
    public Map<String, Object> submitLeave(@RequestBody LeaveRequest leaveRequest) {
        LeaveRequest saved = leaveService.submitLeaveRequest(leaveRequest);

        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "请假申请提交成功");
        response.put("data", saved);
        return response;
    }

    /**
     * 获取待办任务
     */
    @GetMapping("/tasks/{assignee}")
    public Map<String, Object> getTasks(@PathVariable String assignee) {
        List<Task> tasks = leaveService.getTasks(assignee);

        // 转换为简单的Map格式
        List<Map<String, Object>> taskList = tasks.stream()
                .map(task -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("taskId", task.getId());
                    map.put("taskName", task.getName());
                    map.put("assignee", task.getAssignee());
                    map.put("createTime", task.getCreateTime());
                    map.put("processInstanceId", task.getProcessInstanceId());
                    return map;
                })
                .collect(Collectors.toList());

        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("count", taskList.size());
        response.put("data", taskList);
        return response;
    }

    /**
     * 审批请假申请
     */
    @PostMapping("/approve")
    public Map<String, Object> approveLeave(
            @RequestParam String taskId,
            @RequestParam boolean approved,
            @RequestParam(required = false) String comment) {

        leaveService.approveLeave(taskId, approved, comment);

        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "审批完成");
        return response;
    }

    /**
     * 查询所有请假申请
     */
    @GetMapping("/list")
    public Map<String, Object> listAll() {
        List<LeaveRequest> list = leaveService.getAllLeaveRequests();

        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("count", list.size());
        response.put("data", list);
        return response;
    }

    /**
     * 根据员工查询请假申请
     */
    @GetMapping("/list/{employee}")
    public Map<String, Object> listByEmployee(@PathVariable String employee) {
        List<LeaveRequest> list = leaveService.getLeaveRequestsByEmployee(employee);

        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("count", list.size());
        response.put("data", list);
        return response;
    }
}
